package com.jxnd.sunflower.controller;

import com.jxnd.sunflower.common.JSONResult;
import com.jxnd.sunflower.domain.RCollection;
import com.jxnd.sunflower.domain.RGoods;
import com.jxnd.sunflower.domain.dto.UserDTO;
import com.jxnd.sunflower.exception.SunFlowerException;
import com.jxnd.sunflower.mapper.RCollectionMapper;
import com.jxnd.sunflower.utils.JwtToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author: 敷衍
 * @description:
 * @date: 2021/12/6 11:46
 * @version: 1.0
 */
@Api(value = "收藏夹",tags = "收藏夹")
@RestController
@RequestMapping("/collection")
public class CollectionController {


    @Autowired
    private RCollectionMapper collectionMapper;


    @GetMapping("/count")
    @ApiOperation(value = "查看用户收藏数量",notes = "查看用户收藏数量")
    private JSONResult userInfo(@RequestHeader("Authorization") String token){
        int userId = JwtToken.getUserId(token);

        Integer count = collectionMapper.count(userId);

        return new JSONResult(count);
    }

    @GetMapping("/is")
    @ApiOperation(value = "查看用户是否收藏",notes = "查看用户是否收藏")
    private JSONResult iscollection(@RequestHeader("Authorization") String token,Integer goodsId){
        int userId = JwtToken.getUserId(token);

        Integer integer = collectionMapper.queryisCollection(userId, goodsId);

        if(integer==null){
            return new JSONResult("点击收藏");
        }

        return new JSONResult("已收藏");
    }


    @GetMapping("/collectionGoods")
    @ApiOperation(value = "收藏商品",notes = "收藏商品")
    private JSONResult collectionGoods(@RequestHeader("Authorization") String token,Integer goodsId){

        int userId = JwtToken.getUserId(token);

        RCollection collection = new RCollection();
        collection.setUserId(userId);
        collection.setGoodsId(goodsId);
        collection.setCreateTime(new Date());
        collection.setStatus(0);

        int i = collectionMapper.insertCollection(collection);

        if(i>0){
            return new JSONResult("收藏成功");
        }

        return new JSONResult("收藏失败");

    }

    @GetMapping("/mycollection")
    @ApiOperation(value = "查看所有我的收藏",notes = "查看所有我的收藏")
    private JSONResult mycollection(@RequestHeader("Authorization") String token){
        int userId = JwtToken.getUserId(token);
        List<RGoods> rGoods = collectionMapper.queryGoods(userId);

        return new JSONResult(rGoods);

    }


    @GetMapping("/deleteId")
    @ApiOperation(value = "删除收藏",notes = "删除收藏")
    private JSONResult deleteCollection(@RequestHeader("Authorization") String token,Integer id){
        int userId = JwtToken.getUserId(token);

        Integer integer = collectionMapper.deleteCollection(userId, id);

        if(integer==null){
            return new JSONResult("删除失败 联系管理员");
        }
        return new JSONResult("删除成功");


    }




    }
